<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Results_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('result_model', '', TRUE);
        if (!$this->session->userdata('logged_in')) {
            redirect('login');
        }
        
        $this->load->library('authentication');$this->load->helper('t');
        $this->loggedUser = $this->authentication->getLoggedUser();$this->authentication->redirectIfNotBe( get_employee_titles());
    }

    function index($limit = '0') {
        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }

        $data['result'] = $this->result_model->result_list_all($limit);
        $data['group_list'] = $this->result_model->group_list();
        $data['quiz_list'] = $this->result_model->quiz_list();
        $data['title'] = "Result";
        $data['limit'] = $limit;
        $this->load->view('header', $data);
        $this->load->view('result_list_all', $data);
        $this->load->view('footer', $data);
    }

    function get_report() {
        $logged_in = $this->session->userdata('logged_in');
        if ($logged_in['su'] != "1") {
            exit('Permission denied');
            return;
        }
        if ($this->input->post('generate')) {
            $gid = $this->input->post('gid');
            $quid = $this->input->post('quid');
            $data['report'] = $this->result_model->report($gid, $quid);
            $this->load->library('pdf');
            $this->pdf->load_view('get_report', $data);
            $this->pdf->render();
            $filename = date('Y-M-d_H:i:s', time()) . ".pdf";
            $this->pdf->stream($filename);
        } else {
            redirect('result');
        }
    }

    function user($limit = '0') {
        $logged_in = $this->session->userdata('logged_in');
        $user_id = $logged_in['id'];

        $data['result'] = $this->result_model->result_list_all($limit, $user_id);
        $data['title'] = "Result";
        $data['limit'] = $limit;
        $this->load->view('header', $data);
        $this->load->view('result_list_all', $data);
        $this->load->view('footer', $data);
    }

    function view($id, $quid = '') {
        
        
        $logged_in = $this->session->userdata('logged_in');
        // getting the last ten result of all users of particular quiz
        $last_ten_result = $this->result_model->last_ten_result($quid);
        $value = array();
        $value[] = array('Quiz Name', 'Percentage (%)');
        foreach ($last_ten_result as $val) {
            $value[] = array($val['username'] . ' (' . $val['first_name'] . " " . $val['last_name'] . ')', intval($val['percentage']));
        }
        $viewData['value'] = json_encode($value);


//        if ($logged_in['su'] == "1") {
//            $data['result'] = $this->result_model->result_view($id);
//        } else {
            $user_id = $logged_in['id'];
            $viewData['result'] = $this->result_model->result_view($id, $user_id);
//        }


        // getting the individual question time
        $qtime = array();
        $qtime[] = array('Question Number', 'Time in Seconds');
        foreach (explode(",", $viewData['result']->time_spent_ind) as $key => $val) {
            $qtime[] = array("Q " . ($key + 1) . ")", intval($val));
        }
        $viewData['qtime'] = json_encode($qtime);

        $viewData['title'] = "Result";

        $this->load->view(get_folder($this->loggedUser->title).'/result_view', $viewData);
    }

    function view_answer($id) {
        $data['result'] = $this->result_model->result_view($id);
        $data['assigned_question'] = $this->result_model->get_question($id);
        $data['title'] = "Result";
        $data['id'] = $id;
        $this->load->view('header', $data);
        $this->load->view('view_answer', $data);
        $this->load->view('footer', $data);
    }

}

?>
